package com.wxw.tools;

import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;

import com.wxw.controller.GroupChatController;

/**
 * 发布消息的回调类 
 *  ◦必须在回调类中实现三个方法： 
 *  public void messageArrived(String topic, MqttMessage message)
 *  接收已经预订的发布。 
 *  public void connectionLost(Throwable cause)
 *  在断开连接时调用。 
 *  public void deliveryComplete(IMqttDeliveryToken token))
 *  接收到已经发布的 QoS 1 或 QoS 2 消息的传递令牌时调用。
 *  ◦由 MqttClient.connect 激活此回调。
 * @author AFatOrange
 *
 */

public class PushCallback implements MqttCallback {
    private final Logger log = LoggerFactory.getLogger(this.getClass());
    private MqttClient client;
    private MqttConnectOptions options;
    
    @Autowired
    testServer testServer;
    
    
//用于客户端重连
    public PushCallback() {}
    public PushCallback(MqttClient client, MqttConnectOptions options) {
        this.client = client;
        this.options = options;
    }
    /**
     * 连接丢失后，一般在这里面进行重连
     */
    @Override
    public void connectionLost(Throwable cause) {

      log.info("连接断开，开始重连");

//先断开连接再重新连接

        MQTTUtil.disconnect(client);
        MQTTUtil.startReconnect(client,options);

        
    }


    /**
     * 发布publish后会执行到这里,发送状态,接收到已经发布的 QoS 1 或 QoS 2 消息的传递令牌时调用。
     */
    @Override
    public void deliveryComplete(IMqttDeliveryToken token) {
        log.info("deliveryComplete:{}",token.isComplete());
    }
    /**
     * 消息到达：指收到消息,subscribe后得到的消息会执行到这里面
     */
    
    @Override
    public void messageArrived(String topic, MqttMessage message) throws Exception {
        log.info("接收消息主题:{}",topic);
        log.info("接收消息Qos:{}",message.getQos());
        String content = new String(message.getPayload(), "utf-8");
        log.info("接收消息内容:{}",content);
         
		//testServer.testServer1(topic, content);
    }
}